package com.millstein.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.millstein.eduservice.entity.EduChapter;
import com.millstein.eduservice.entity.EduVideo;
import com.millstein.eduservice.entity.chapter.ChapterVo;
import com.millstein.eduservice.entity.chapter.VideoVo;
import com.millstein.eduservice.mapper.EduChapterMapper;
import com.millstein.eduservice.mapper.EduVideoMapper;
import com.millstein.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.millstein.servicebase.handler.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.management.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-15
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduChapterMapper eduChapterMapper;

    @Autowired
    private EduVideoMapper eduVideoMapper;

    /**
     * 根据课程id查询章节小节的信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        List<ChapterVo> voList = new ArrayList<>();
        Map<String, Object> relateMap = new HashMap<>();
        // 1.根据课程id查询所有的chapter
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderBy(true, true, "sort");
        List<EduChapter> chapters = eduChapterMapper.selectList(wrapper);
        for (int i = 0; i < chapters.size(); i++) {
            EduChapter chapter = chapters.get(i);
            ChapterVo vo = new ChapterVo();
            BeanUtils.copyProperties(chapter, vo);
            voList.add(vo);
            relateMap.put(vo.getId(), i);
        }
        // 2.根据课程id查询所有的video
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", courseId);
        wrapper2.orderBy(true, true, "sort");
        List<EduVideo> videos = eduVideoMapper.selectList(wrapper2);
        for (EduVideo video : videos) {
            VideoVo vo = new VideoVo();
            BeanUtils.copyProperties(video, vo);
            voList.get((Integer) relateMap.get(video.getChapterId())).getChildren().add(vo);
        }

        return voList;
    }

    /**
     * 根据id删除章节
     * @param chapterId
     */
    @Override
    public void deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        // selectCount直接查数量
        int count = eduVideoMapper.selectCount(wrapper);
        if (count != 0) {
            throw new GuliException(20001, "该章节下还存在小节，不能被删除");
        }

        boolean result = this.removeById(chapterId);
        if (!result) {
            throw new GuliException(20001, "删除失败");
        }
    }
}
